package com.kang.controller;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import com.kang.bean.Users;
import com.kang.commons.Result;
import com.kang.dto.UsersDto;
import com.kang.service.UserService;
import com.kang.vo.UsersVo;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: service
 * @description: 用户控制层
 * @author: K.faWu
 * @create: 2022-02-17 16:45
 **/
@RestController
@RequestMapping("/user")
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/{id}")
    public Result<?> getUser(@PathVariable("id") Long id) {
        Users user = userService.getById(id);
        return Result.success(user);
    }

    /**
     * 后台使用展示用户列表
     */
    @PostMapping("/getList")
    public Result<?> getList(@RequestBody UsersDto usersDto, Pageable page, HttpServletRequest request) {
        PageInfo<UsersVo> pageInfo = userService.getList(usersDto, page, request);
        if (pageInfo.getList() == null || pageInfo.getList().size() == 0) {
            return Result.failure("发现越权行为！对用户及ip进行进行警告！");
        }
        return Result.success(pageInfo);
    }

    /**
     * 获取登录的用户
     */
    @PostMapping
    public Result<Users> getUsers(@RequestBody String token) {
        Users users = userService.getUsers(token);
        return Result.success(users);
    }

    /**
     * 更新头像
     */
    @PostMapping("/upUserHead")
    public Result<?> upUserHead(@RequestBody UsersDto usersDto) {
        userService.upUserHead(usersDto);
        return Result.success();
    }

    /**
     * 头像上传
     */
    @PostMapping("/upload")
    public Result<?> upload(@RequestParam MultipartFile file) {
        String path = userService.upload(file);
        return Result.success(path);
    }

    /**
     * 注册时更新用户信息，通过type判断注册类型
     */
    @PostMapping("/update")
    public Result<?> updateByType(@RequestBody UsersDto usersDto) {
        String result = userService.updateByType(usersDto);
        return Result.success(result);
    }

    /**
     * 登录用验证码
     *
     * @param usersDto 登录的用户信息
     * @return 登录成功的信息
     */
    @PostMapping("/public/getLoginCode")
    public Result<?> getLoginCode(@RequestBody UsersDto usersDto) {
        userService.getLoginCode(usersDto);
        return Result.success();
    }

    @PostMapping("/public/login")
    public Result<?> login(@RequestBody UsersDto usersDto) {
        UsersVo login = userService.login(usersDto);
        return Result.success(login);
    }

    /**
     * 注册用户
     */
    @PostMapping("/public/register")
    public Result<?> register(@RequestBody UsersDto usersDto) {
        userService.register(usersDto);
        return Result.success();
    }

    /**
     * 注册验证码
     */
    @PostMapping("/public/register/getVerificationCode")
    public Result<?> getVerificationCode(@RequestBody UsersDto usersDto) {
        String resutl = userService.getVerificationCode(usersDto);
        if (StringUtils.isEmpty(resutl)) {
            return Result.success();
        }
        return Result.failure(resutl);
    }
}
